#! /usr/bin/env python
# -*- coding: utf-8 -*-
#
# Copyright (c) 2011 BidiX (http://bidix.info)
#
#   Licensed under the Apache License, Version 2.0 (the "License");
#   you may not use this file except in compliance with the License.
#   You may obtain a copy of the License at
#
#	 http://www.apache.org/licenses/LICENSE-2.0
#
#   Unless required by applicable law or agreed to in writing, software
#   distributed under the License is distributed on an "AS IS" BASIS,
#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#   See the License for the specific language governing permissions and
#   limitations under the License.
#
"""
test unit for notewikiapp_client and gae_client.

Uses 2 notewikiapp:
	- http access notewikiapp
	- https access notewikiapp

"""
import unittest
import sys, os, os.path
from gae_client import GAE_Client
from notewikiapp_client import Notewikiapp_Client

# global variables positionned by main function
http_notewikiapp = ''
http_notewikiapp_domain = ''
http_notewikiapp_user = ''
http_notewikiapp_password = ''
https_notewikiapp = ''
https_notewikiapp_domain = ''
https_notewikiapp_user = ''
https_notewikiapp_password = ''

class TestFunctions(unittest.TestCase):
	"""
	Sequence of test functions
	"""
	COOKIE_FILENAME="test_cookies"
	def _delete_cookiefile(self):
		"delete cookie_jar if present."
		if os.path.exists(TestFunctions.COOKIE_FILENAME):
			os.remove(TestFunctions.COOKIE_FILENAME)
	
	def setUp(self):
		"no cookie_jar present"
		self._delete_cookiefile()

	def test_local_http_get_login(self):
		"local http get login required"
		self._delete_cookiefile()
		gae_client = GAE_Client(http_notewikiapp_user, http_notewikiapp_password, http_notewikiapp_domain, http_notewikiapp, TestFunctions.COOKIE_FILENAME, local=True, local_admin=True, scheme='http')
		(response_code, response_body) = gae_client.request('GET', '/_admin')
		self.assertEqual(200, response_code)
		self.assertTrue(response_body.find('<title>Notewiki: Notewiki administration</title>') != -1)

	def test_remote_http_get_no_login(self):
		"remote http get no login required"
		self._delete_cookiefile()
		gae_client = GAE_Client(http_notewikiapp_user, '', http_notewikiapp_domain, http_notewikiapp, TestFunctions.COOKIE_FILENAME, local=False, local_admin=True, scheme='http')
		(response_code, response_body) = gae_client.request('GET', '/notewiki/doc/overview')
		self.assertEqual(200, response_code)
		self.assertTrue(response_body.find('<title>Notewiki: Overview</title>') != -1)

	def test_remote_https_get_no_login(self):
		"remote https get no login required"
		self._delete_cookiefile()
		gae_client = GAE_Client(https_notewikiapp_user, '', https_notewikiapp_domain, https_notewikiapp, TestFunctions.COOKIE_FILENAME, local=False, local_admin=True, scheme='https')
		(response_code, response_body) = gae_client.request('GET', '/notewiki/doc/overview')
		self.assertEqual(200, response_code)
		self.assertTrue(response_body.find('<title>Notewiki: Overview</title>') != -1)

	def test_remote_http_get_bad_login(self):
		"remote http get login required. Bad login"
		self._delete_cookiefile()
		gae_client = GAE_Client(http_notewikiapp_user, '', http_notewikiapp_domain, http_notewikiapp, TestFunctions.COOKIE_FILENAME, local=False, local_admin=True, scheme='http')
		(response_code, response_body) = gae_client.request('GET', '/_admin')
		self.assertEqual(401, response_code)
		

	def test_remote_https_get_bad_login(self):
		"remote https get login required. Bad login"
		self._delete_cookiefile()
		gae_client = GAE_Client(https_notewikiapp_user, '', https_notewikiapp_domain, https_notewikiapp, TestFunctions.COOKIE_FILENAME, local=False, local_admin=True, scheme='https')
		(response_code, response_body) = gae_client.request('GET', '/_admin')
		self.assertEqual(401, response_code)

	def test_remote_http_get_login(self):
		"remote http get login required"
		self._delete_cookiefile()
		gae_client = GAE_Client(http_notewikiapp_user, http_notewikiapp_password, http_notewikiapp_domain, http_notewikiapp, TestFunctions.COOKIE_FILENAME, local=False, local_admin=True, scheme='http')
		(response_code, response_body) = gae_client.request('GET', '/_admin')
		self.assertEqual(200, response_code)
		self.assertTrue(response_body.find('<title>Notewiki: Notewiki administration</title>') != -1)

	def test_remote_https_get_login(self):
		"remote https get login required"
		self._delete_cookiefile()
		gae_client = GAE_Client(https_notewikiapp_user, https_notewikiapp_password, https_notewikiapp_domain, https_notewikiapp, TestFunctions.COOKIE_FILENAME, local=False, local_admin=True, scheme='https')
		(response_code, response_body) = gae_client.request('GET', '/_admin')
		self.assertEqual(200, response_code)
		self.assertTrue(response_body.find('<title>Notewiki: Notewiki administration</title>') != -1)

	def test_notewikiapp_get_private_article(self):
		"remote https get article in private area"
		self._delete_cookiefile()
		notewikiapp_client = Notewikiapp_Client(https_notewikiapp, config_filename='test_notewikiapp_client.config')
		(response_code, response_body) = notewikiapp_client.request('GET', '/_admin')
		self.assertEqual(200, response_code)
		self.assertTrue(response_body.find('<title>Notewiki: Notewiki administration</title>') != -1)
	
	def test_notewikiapp_post_private_article(self):
		"remote https post article in private area"
		self._delete_cookiefile()
		notewikiapp_client = Notewikiapp_Client(https_notewikiapp, config_filename='test_notewikiapp_client.config')
		# create article
		notewikiapp_client.post_article('_admin/test_article', 'Test article', "text of article", 'new, test')
		# get article
		(response_code, response_body) = notewikiapp_client.get_article('/_admin/test_article')
		self.assertEqual(200, response_code)
		self.assertTrue(response_body.find('<title>Notewiki: Test article</title>') != -1)
		# update article
		notewikiapp_client.post_article('_admin/test_article', 'Test article', "New text of article", 'new, test')
		# get article in txt format
		(response_code, response_body) = notewikiapp_client.get_article('/_admin/test_article.txt')
		self.assertEqual(200, response_code)
		self.assertEqual('New text of article', response_body)
		# delete article
		(response_code, response_body) = notewikiapp_client.delete_article('/_admin/test_article')
		# get article
		(response_code, response_body) = notewikiapp_client.get_article('/_admin/test_article')
		self.assertEqual(404, response_code)

def main(args):
	"""
	Test unit main function.
	
	Usage:
		test.py http_notewikiapp http_notewikiapp_user http_notewikiapp_password 
				https_notewikiapp https_notewikiapp_user https_notewikiapp_password
			
	"""
	global http_notewikiapp, http_notewikiapp_domain, http_notewikiapp_user, http_notewikiapp_password
	global https_notewikiapp,  https_notewikiapp_domain, https_notewikiapp_user, https_notewikiapp_password
	if len(args) != 9:
		print 'Error: Exactly 8 arguments required.'
		print main.__doc__
		sys.exit(1)

	http_notewikiapp = args[1]
	http_notewikiapp_domain = args[2]
	http_notewikiapp_user = args[3]
	http_notewikiapp_password = args[4]
	https_notewikiapp = args[5]
	https_notewikiapp_domain = args[6]
	https_notewikiapp_user = args[7]
	https_notewikiapp_password = args[8]

	sys.argv = [__file__]	# to remove args from sys.argv
	unittest.main(verbosity=3)	

if __name__ == '__main__':
	main(sys.argv)